myApp.controller('sale', ['$scope', '$http', '$shop', '$employee', function($scope, $http ,$shop, $employee){
	
	$scope.start = "shade";
	$scope.startBtn = "btn btn-default";
	$scope.startBtnState = "开始销售";
	$scope.customer = "common"; 
	$scope.customerNo = ""; 
	$scope.barcode = ""; 
	$scope.quantity = 1; 
	$scope.selectedItem = []; 
	$scope.payType = "1"; 
	$scope.remark = "无"; 
	$scope.total = 0; 
	$scope.receivablesAmount = ""; 
	$scope.returnAmount = ""; 
	$scope.balanceFlag = false;
	var saleOrderNo = "";

	$scope.startSale = function() {
		$scope.start = "notShade";
		$scope.startBtn = "btn btn-warning";
		$scope.startBtnState = "销售中......";
	};

	$scope.addItem = function() {
		if($scope.customer === "common" && $scope.customerNo !== "") {
			alert("请选择会员或者清空会员号");
			return;
		}
		if($scope.customer === "member" && $scope.customerNo === "") {
			alert("请填写会员号");
			return;
		}
		if($scope.barcode === "") {
			alert("请填写条形码");
			return;
		}
		if($scope.quantity === "" || $scope.quantity === null || $scope.quantity === 0) {
			alert("请填写数量");
			return;
		}

		for (var i = 0; i < $scope.selectedItem.length; i++) {
			if($scope.selectedItem[i].barcode === $scope.barcode) {
				$scope.selectedItem[i].quantity += $scope.quantity;
				$scope.selectedItem[i].subTotal += $scope.quantity * $scope.selectedItem[i].salePrice;
				$scope.total += $scope.quantity * $scope.selectedItem[i].salePrice;
				return;
			}
		}

		var itemData = {"barcode": $scope.barcode};

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('showSelectedItem', itemData, postCfg).then(function successCallback(response) {
			var data = response.data[0];
			var item = {};
			item.barcode = $scope.barcode;
			item.name = data.name;
			item.salePrice = data.price;
			item.quantity = $scope.quantity;
			item.subTotal = $scope.quantity * data.price;
			$scope.selectedItem.push(item);

			$scope.total += $scope.quantity * data.price;

			$scope.barcode = ""; 
			$scope.quantity = 1; 

			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("选择该商品操作失败！");
			console.log("fail：" + response);
		});

	};

	$scope.balance = function() {
		$scope.returnAmount = $scope.receivablesAmount - $scope.total; 

		// var itemsJson = "";
		// for (var i = 0; i < $scope.selectedItem.length; i++) {
		// 	if($scope.selectedItem.length === 1) {
		// 		itemsJson += '[';
		// 		itemsJson += JSON.stringify($scope.selectedItem[i]) + "]";
		// 	}else if(i === 0) {
		// 		itemsJson += '[';
		// 		itemsJson += JSON.stringify($scope.selectedItem[i]) + ",";
		// 	}else if(i === ($scope.selectedItem.length-1)) {
		// 		itemsJson += JSON.stringify($scope.selectedItem[i]);
		// 		itemsJson += ']';
		// 	}else {
		// 		itemsJson += JSON.stringify($scope.selectedItem[i]) + ",";
		// 	} 			
		// }

		var itemData = {
			"saleOrder.customerNo":$scope.customerNo, "saleOrder.salespersonNo":$employee.currentEmployee.userName, "saleOrder.shopId":$shop.myShop.shopId, "saleOrder.total":$scope.total, "saleOrder.remark":$scope.remark,
			"saleOrderItemsJson":JSON.stringify($scope.selectedItem), 
			"payment.paymentNo":"001", "payment.amount":$scope.total, "payment.payType":parseInt($scope.payType), "payment.payTransactionNo":"001"
		};
		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('processSale', itemData, postCfg).then(function successCallback(response) {
			saleOrderNo = response.data[0].saleOrderNo;

			$scope.balanceFlag = true;
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("交易操作失败！");
			console.log("fail：" + response);
		});
	};

	$scope.completed = function() {
		$scope.start = "shade";
		$scope.startBtn = "btn btn-default";
		$scope.startBtnState = "开始销售";
		$scope.customer = "common"; 
		$scope.customerNo = ""; 
		$scope.barcode = ""; 
		$scope.quantity = 1; 
		$scope.selectedItem = []; 
		$scope.payType = "1"; 
		$scope.remark = "无"; 
		$scope.total = 0; 
		$scope.receivablesAmount = ""; 
		$scope.returnAmount = ""; 
		$scope.balanceFlag = false;

		alert(
			"订单编号：" + saleOrderNo);

	};

	$scope.cancle = function() {
		$scope.start = "shade";
		$scope.startBtn = "btn btn-default";
		$scope.startBtnState = "开始销售";
		$scope.customer = "common"; 
		$scope.customerNo = ""; 
		$scope.barcode = ""; 
		$scope.quantity = 1; 
		$scope.selectedItem = []; 
		$scope.payType = "1"; 
		$scope.remark = "无"; 
		$scope.total = 0; 
		$scope.receivablesAmount = ""; 
		$scope.returnAmount = ""; 
		$scope.balanceFlag = false;
	};



}]);